package ${controllerPackageName};

import ${modelPackageName}.${modelName?cap_first};
import ${servicePackageName}.I${modelName?cap_first}Service;
import com.dpstudio.dev.core.R;
import net.ymate.platform.core.beans.annotation.Inject;
import net.ymate.platform.persistence.IResultSet;
import net.ymate.platform.validation.validate.VRequired;
import net.ymate.platform.webmvc.annotation.Controller;
import net.ymate.platform.webmvc.annotation.RequestMapping;
import net.ymate.platform.webmvc.annotation.RequestParam;
import net.ymate.platform.webmvc.base.Type;
import net.ymate.platform.webmvc.util.WebResult;
import net.ymate.platform.webmvc.view.IView;


/**
* @Author: CurdGenerateImpl.
* @Date: ${lastUpdateTime?string("yyyy/MM/dd")}.
* @Time: ${lastUpdateTime?string("HH:mm:ss")}.
* @Description: ${modelName?cap_first}Controller generated By CurdGenerateImpl on ${lastUpdateTime?string("yyyy/MM/dd a HH:mm:ss")}
*/
@Controller
@RequestMapping("/${modelName?uncap_first}")
public class ${modelName?cap_first}Controller {

    @Inject
    private I${modelName?cap_first}Service i${modelName?cap_first}Service;


    /**
    * 添加
    *
    <#list fieldList as field>
        <#if (field.varName!"id") != 'id'>
    * @param ${field.varName} ${field.remarks}
        </#if>
    </#list>
    * @return 添加成功的数据信息
    * @throws Exception 添加异常
    */
    @RequestMapping(value = "/create", method = Type.HttpMethod.POST)
    public IView create(<#list fieldList as field><#if (field.varName!"id") != 'id'>@RequestParam ${field.varType} ${field.varName}<#if field_has_next>, </#if></#if></#list>) throws Exception {
        R r = i${modelName?cap_first}Service.create(<#list fieldList as field><#if (field.varName!"id") != 'id'>${field.varName}<#if field_has_next>, </#if></#if></#list>);
        return r.json();
    }

    /**
    * 修改
    *
    <#list fieldList as field>
        * @param ${field.varName} ${field.remarks}
    </#list>
    * @return 修改成功的数据信息
    * @throws Exception 修改异常
    */
    @RequestMapping(value = "/update", method = Type.HttpMethod.POST)
    public IView create(<#list fieldList as field>@RequestParam ${field.varType} ${field.varName}<#if field_has_next>, </#if></#list>) throws Exception {
        R r = i${modelName?cap_first}Service.update(<#list fieldList as field>${field.varName}<#if field_has_next>, </#if></#list>);
        return r.json();
    }

    /**
    * 删除
    *
    * @param ids 要删除的id数组
    * @throws Exception 删除异常
    */
    @RequestMapping(value = "/delete", method = Type.HttpMethod.POST)
    public IView delete(@VRequired(msg = "ids不能为空")
                        @RequestParam("ids[]") String[] ids) throws Exception {
        R r = i${modelName?cap_first}Service.delete(ids);
        return r.json();
    }

    /**
    * 根据id查询
    *
    * @param id     数据id
    * @return 查询的数据
    * @throws Exception 查询异常
    */
    @RequestMapping(value = "/detail", method = Type.HttpMethod.GET)
    public IView detail(@VRequired(msg = "id不能为空")
                        @RequestParam String id) throws Exception {
        ${modelName?cap_first} ${modelName?uncap_first}Bean = i${modelName?cap_first}Service.findById(id);
        return WebResult.succeed().data(${modelName?uncap_first}Bean).toJSON();
    }

    /**
    * 查询所有
    *
    <#list fieldList as field>
        * @param ${field.varName} ${field.remarks}
    </#list>
    * @param page     第几页
    * @param pageSize 每页显示多少条
    * @return 查询的数据
    * @throws Exception 查询异常
    */
    @RequestMapping(value = "/list", method = Type.HttpMethod.GET)
    public IView list(<#list fieldList as field>@RequestParam ${field.varType} ${field.varName}<#if field_has_next>, </#if></#list>,@RequestParam(defaultValue = "1") int page,@RequestParam(defaultValue = "10") int pageSize) throws Exception {

        IResultSet<${modelName?cap_first}> resultData = i${modelName?cap_first}Service.findAll(<#list fieldList as field>${field.varName}<#if field_has_next>, </#if></#list>, page, pageSize);
        return WebResult.succeed().attr("data", resultData.getResultData())
        .attr("total", resultData.getRecordCount())
        .attr("page", page).toJSON();
    }

}
